<template>
  <div class="uploadImage">
    <div
      class="demo-upload-list"
      v-for="(item, index) in uploadList"
      :key="index"
    >
      <template v-if="item.status === 'finished'">
        <img :src="item.url" />
        <div class="demo-upload-list-cover">
          <Icon type="ios-eye-outline" @click.native="handleView(item)"></Icon>
          <Icon
            type="ios-trash-outline"
            @click.native="handleRemove(item)"
          ></Icon>
        </div>
      </template>
      <template v-else>
        <Progress
          v-if="item.showProgress"
          :percent="item.percentage"
          hide-info
        ></Progress>
      </template>
    </div>
    <Upload
      ref="upload"
      :show-upload-list="false"
      :default-file-list="defaultList"
      :on-success="handleSuccess"
      :format="['jpg', 'jpeg', 'png']"
      accept=".jpg,.jpeg,.png"
      :max-size="2048"
      :on-format-error="handleFormatError"
      :on-exceeded-size="handleMaxSize"
      :before-upload="beforeUpload"
      type="drag"
      :action="uploadObj.actionUrl"
      :headers="uploadObj.headers"
      style="display: inline-block; width: 58px"
    >
      <div style="width: 58px; height: 58px; line-height: 58px">
        <Icon type="ios-camera" size="20"></Icon>
      </div>
    </Upload>
    <Modal title="预览图片" v-model="visible" :mask-closable="false">
      <img :src="bigUrl" v-if="visible" style="width: 100%" />
    </Modal>
  </div>
</template>
<script>
import config from "@/config";
import { getToken } from "@/libs/util";
export default {
  props: {
    // 真正的值
    value: {
      type: [String, Array],
      default: () => "",
    },
    // 给校验字段赋值 用来校验
    checkField: {
      type: [String, Array],
      default: () => "",
    },
    // 校验字段名
    fieldName: {
      type: String,
      default: () => "",
    },
    // 删除字段赋值
    deleteFileIds: {
      type: Array,
      default: () => [],
    },
    // 默认回显
    defaultList: {
      type: Array,
      default: () => [],
    },
    // 是否校验并
    isCheckField: {
      type: Boolean,
      default: () => true,
    },
  },
  data() {
    return {
      uploadObj: {
        actionUrl:
          (process.env.NODE_ENV === "development"
            ? config.baseUrl.dev
            : process.env.NODE_ENV === "testing"
            ? config.baseUrl.test
            : config.baseUrl.pro) + "/platform-file/file/img/upload",
        headers: {
          Authorization: getToken(),
        },
      },
      visible: false,
      bigUrl: "",
      uploadList: [],
      obj: {},
    };
  },
  mounted() {
    setTimeout(() => {
      this.setDefault();
    }, 200);
  },
  methods: {
    setDefault() {
      this.$nextTick(function () {
        this.uploadList = this.$refs.upload.fileList;
      });
    },
    // 预览
    handleView(item) {
      this.bigUrl = item.url;
      this.visible = true;
    },
    // 删除
    handleRemove(file) {
      const fileList = this.$refs.upload.fileList;
      this.$refs.upload.fileList.splice(fileList.indexOf(file), 1);
      // 添加的时候删除
      if (file.response) {
        this.$emit("input", "");
      } else {
        // 从defaultList中查到和file.url相同的数据 然后拿到id 抛出去
        var ids = [...this.deleteFileIds, this.obj.bannerPic[0].fileId].filter(
          (it) => it
        );
        this.$emit("update:deleteFileIds", ids);
      }
      // 是否校验
      if (this.isCheckField) {
        // 设置校验值
        this.$emit("update:checkField", "");
        this.$parent.$parent.validateField(this.fieldName);
      }
    },
    // 上传成功
    handleSuccess(res, file) {
      file.url = res.data[0].fileAddress;
      file.name = res.data[0].oldFileName;
      // 设置真正的值
      this.$emit("input", res.data[0].id);
      // 抛出src
      this.$emit("setSrc", res.data[0].fileAddress);
      // 是否校验
      if (this.isCheckField) {
        // 设置校验值
        this.$emit("update:checkField", res.data[0].id);
        this.$parent.$parent.validateField(this.fieldName);
      }
    },
    // 校验格式
    handleFormatError(file) {
      this.$Message.error("上传图片格式不正确!");
    },
    // 大小限制
    handleMaxSize(file) {
      this.$Message.error("上传图片大小不能超过 10MB!");
    },
    beforeUpload() {
      const check = this.uploadList.length < 1;
      if (!check) {
        this.$Message.error("只能上传一张图片");
      }
      return check;
    },
  },
};
</script>
<style lang="scss" scoped>
.demo-upload-list {
  display: inline-block;
  width: 60px;
  height: 60px;
  text-align: center;
  line-height: 60px;
  border: 1px solid transparent;
  border-radius: 4px;
  overflow: hidden;
  background: #fff;
  position: relative;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
  margin-right: 4px;
}
.demo-upload-list img {
  width: 100%;
  height: 100%;
}
.demo-upload-list-cover {
  display: none;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.6);
}
.demo-upload-list:hover .demo-upload-list-cover {
  display: block;
}
.demo-upload-list-cover i {
  color: #fff;
  font-size: 20px;
  cursor: pointer;
  margin: 0 2px;
}
</style>
